package GameMain.View.Room
{
	import Engine.Module.ViewBase;
	
	import GameMain.Game;
	import GameMain.Macro.MacroEvent;
	import GameMain.Macro.Macro;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import library.SocketData.SocketData;
	
	
	public class Room extends ViewBase
	{
		
		private var _playerBoxList:Object = {};
		private var _roomMasterID:int = 0;
		private var _zhuBei_mc:MovieClip;
		private var _start_mc:MovieClip;
		private var _cuoHe_mc:MovieClip;
		private var _cuoHeTimer:Timer = new Timer(1000);
		private var _cuoHe_txt:TextField;
		
		public function Room(p_content:Sprite)
		{
			super(p_content);
			Game.ControlMgr.getButton(this._content["YAO_QING"]);
			Game.ControlMgr.getButton(this._content["ZHUN_BEI"]).setOnClickFun(onReady);
			Game.ControlMgr.getButton(this._content["START_GAME"]).setOnClickFun(startGame);
			
			this.initUI();
		}
		
		private function initUI():void
		{
			
			this._zhuBei_mc = this._content["ZHUN_BEI"] as MovieClip;
			this._zhuBei_mc.visible = false;
			this._start_mc = this._content["START_GAME"] as MovieClip;
			
			this._cuoHe_mc = this._content["CUO_HE"] as MovieClip;
			this._cuoHe_mc.visible = false;
			this._cuoHe_txt = this._cuoHe_mc["CUOHE_TXT"] as TextField;
			
			this._cuoHeTimer.addEventListener(TimerEvent.TIMER,onCuoHeTimerE);
			
			var pBox:PlayerBox;
			pBox = new PlayerBox(this._content["PLAYER_1"]);
			pBox.setPlayerPosIndex(1);
			this._playerBoxList[1] = pBox;
			pBox = new PlayerBox(this._content["PLAYER_2"]);
			pBox.setPlayerPosIndex(2);
			this._playerBoxList[2] = pBox;
			pBox = new PlayerBox(this._content["PLAYER_3"]);
			pBox.setPlayerPosIndex(3);
			this._playerBoxList[3] = pBox;
			pBox = new PlayerBox(this._content["PLAYER_4"]);
			pBox.setPlayerPosIndex(4);
			this._playerBoxList[4] = pBox;;
		}
		
		private function onReady():void
		{
			var data:SocketData = new SocketData();
			data.writeInt8(Game.ownPlayer.playerRoomIndex);
			this.printLog(Game.ownPlayer.playerRoomIndex);
			if(this._zhuBei_mc.currentFrame == 1)
				data.writeInt8(PlayerBox.STATE_READY);
			else
				data.writeInt8(PlayerBox.STATE_NORMAL);
			this.sendMsg(MacroEvent.MSG_TO_SERVER_SET_READY,data);
		}
		
		private function startGame():void
		{
			if(this._start_mc.currentFrame == 1)
				this.sendMsg(MacroEvent.MSG_TO_SERVER_START_GAME,null);
			else
				this.sendMsg(MacroEvent.MSG_TO_SERVER_CANCEL_GAME,null);
		}
		
		public function onJoinRoom(data:SocketData):void
		{
			var rID:int = data.readInt16();
			var rName:String = data.readString();
			this._content["ROOM_NAME"].text = String(rID)//+" 名字:"+rName;
			this._roomMasterID = data.readInt16();
			var rMax:int = data.readInt8();
			var rCNum:int = data.readInt8();
			for(var i:int =1;i<=4;i++)
			{
				var pBox:PlayerBox = this._playerBoxList[i];
				pBox.setBoxState(data.readInt8());
			}
			this.setMaserter();
		}
		
		private function setMaserter():void
		{
			if(this._roomMasterID == Game.ownPlayer.entityID)
			{
				this._start_mc.visible = true;
				this._zhuBei_mc.visible = false;
				this.setPlayerBoxClick(true);
			}
			else
			{
				this._start_mc.visible = false;
				this._zhuBei_mc.visible = true;
				this.setPlayerBoxClick(false);
			}
		}
		
		private function onSetPlayerInfo(data:SocketData):void
		{
			var index:int = data.readInt8();
			var pBox:PlayerBox = this._playerBoxList[index];
			pBox.setPlayerInfo(data);
			if(pBox.entityID == this._roomMasterID)
			{
				pBox.setMaster(1);
			}
			else
				pBox.setMaster(0);
			if(pBox.entityID == Game.ownPlayer.entityID)
				pBox.content.mouseEnabled = false;
		}
		
		private function setPlayerBoxClick(b:Boolean):void
		{
			for(var i:int = 1;i<= 4;i++)
			{
				var pBox:PlayerBox = this._playerBoxList[i];
				if(b)
					pBox.content.mouseEnabled = true;
				else
					pBox.content.mouseEnabled = false;
			}
		}
		
		private function onSetPlayerState(data:SocketData):void
		{
			var index:int = data.readInt8();
			var state:int = data.readInt8();
			var pBox:PlayerBox = this._playerBoxList[index];
			pBox.setReady(state);
			if(pBox.entityID == Game.ownPlayer.entityID)
			{
				this.setReady(pBox.isReady)
			}
		}
		
		private function onPlayerLeave(data:SocketData):void
		{
			var index:int = data.readInt8();
			var pBox:PlayerBox = this._playerBoxList[index];
			pBox.setWaiting();
		}
		
		private function setReady(b:Boolean):void
		{
			if(b)
				this._zhuBei_mc.gotoAndStop(2);
			else
				this._zhuBei_mc.gotoAndStop(1);
		}
		
		private function onStartGame(data:SocketData):void
		{
			this._cuoHe_txt.text = "0";
			if(this._start_mc.visible)
			{
				this._start_mc.gotoAndStop(2);
			}
			this._cuoHeTimer.start();
			this._cuoHe_mc.visible = true;
		}
		
		private function onCancelGame(data:SocketData):void
		{
			if(this._start_mc.visible)
			{
				this._start_mc.gotoAndStop(1);
			}
			this._cuoHeTimer.stop();
			this._cuoHe_mc.visible = false;
		}
		
		private function onCuoHeTimerE(e:TimerEvent):void
		{
			this._cuoHe_txt.text = String(int(this._cuoHe_txt.text) + 1)
		}
		
		override protected function onShowBack():void
		{
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_ROOM_PLAYER,onSetPlayerInfo);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_ROOM_STATE,onSetPlayerState);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_LEAVE_ROOM,onPlayerLeave);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_START_GAME,onStartGame);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_CANCEL_GAME,onCancelGame);
			Game.ModuleMgr.getModule(Macro.MODULE_WAIT_GAME)["onRegesterWaitGame"]();
		}
		
		override protected function onDisposeBack():void
		{
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_ROOM_PLAYER);
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_ROOM_STATE);
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_LEAVE_ROOM);
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_START_GAME);
			Game.EngineMgr.destroyEvent(MacroEvent.MSG_FROM_SERVER_CANCEL_GAME);
			this.onCancelGame(null);
		}
	}
}